﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using L.M.System.Application.IServices;
using L.M.System.Domain.DTO;
using L.M.System.Domain.Entity;
using L.M.System.Infrastruecure.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace L.M.System.Application.Services
{
    /// <summary>
    /// 基于角色的按钮权限添加
    /// </summary>
    public class Role_button_permissionsServices : BaseServices<role_button_permissions, string>, IRole_button_permissionsServices
    {
        private readonly IRole_button_permissionsRepository role_Button_PermissionsRepository;

        public Role_button_permissionsServices(IRole_button_permissionsRepository role_Button_PermissionsRepository) : base(role_Button_PermissionsRepository)
        {
            this.role_Button_PermissionsRepository = role_Button_PermissionsRepository;
        }

        /// <summary>
        /// 获取该角色的所有按钮权限进行反填
        /// </summary>
        /// <param name="rId">角色编号</param>
        /// <returns>获取该角色的所有按钮权限进行反填</returns>
        public async Task<ApiReturn<string>> GetRoleButtonInfor(string? rId)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var list = (await role_Button_PermissionsRepository.GetShowQueryableAsync(c => c.role_number == rId)).ToList();
            var buttonInfor = "";
            foreach (var item in list)
            {
                buttonInfor += item.authinfor_number + "_" + item.button_number + ",";
            }
            apiReturn.Code = 200;
            apiReturn.Message =  buttonInfor.TrimEnd(',');
            return apiReturn;
        }

        /// <summary>
        /// 获取每个界面的权限
        /// </summary>
        /// <param name="roleButton"></param>
        /// <returns>获取每个界面的权限</returns>
        public async Task<ApiReturn<string>> GetRoleNumber(RoleButton roleButton)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            //先找关于当前角色页面的所有按钮权限
            var role = (await role_Button_PermissionsRepository.GetShowQueryableAsync(c => c.role_number == roleButton.role_number && c.authinfor_number == roleButton.auinfor_number)).ToList();
            string? res = "";
            foreach (var item in role)
            {
                res += item.button_number + "_";
            }
            apiReturn.Code = 200;
            apiReturn.Message = res.TrimEnd('_');
            return apiReturn;
        }
    }
}
